package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import utils.FileUtil;
import utils.StringUtil;

public class BeanUtil {

	private String filePath;
	private String fileName;

	public BeanUtil(String filePath, String fileName) {
		this.filePath = filePath;
		this.fileName = fileName;
	}

	public void generate() throws Exception {

		InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(filePath, fileName + ".txt")), "UTF-8");
		BufferedReader br = new BufferedReader(isr);
		StringBuffer sb = new StringBuffer();
		sb.append("package entities;");
		sb.append("\n");
		sb.append("import java.io.Serializable;");
		sb.append("\n");
		sb.append("import java.util.Date;");
		sb.append("\n");
		sb.append("import basic.BaseEntity;");
		sb.append("\n");
		sb.append("import basic.FieldMeta;");
		sb.append("\n");
		sb.append("\n");
		sb.append("public class " + fileName + " extends BaseEntity implements Serializable {");
		sb.append("\n");
		sb.append("\n");
		sb.append("private static final long serialVersionUID = 1L;");
		sb.append("\n");
		sb.append("\n");
		
		int seq = 0;
		String line = null;
		while ((line = br.readLine()) != null) {
			String[] s = line.split("\\s+");
			String name = s[0];
			String column = s[1];
			String type = "String";
			String length = "0";
			boolean needLength = false;
			boolean needPrimary = false;
			boolean needRequire = false;
			if (s[2].startsWith("varchar")) {
				needLength = true;
				length = s[2].substring((s[2].indexOf("(")+1), s[2].indexOf(")"));
			}
			if (s[2].startsWith("numeric")) {
				type = "Double";
			}
			if (s[2].startsWith("datetime")) {
				type = "Date";
			}
			if (s[3].equalsIgnoreCase("TRUE")) {
				needPrimary = true;
			}
			if (s[5].equalsIgnoreCase("TRUE")) {
				needRequire = true;
			}

			sb.append("@FieldMeta(");
			if (needPrimary) {
				sb.append("primary = true,");
			}
			sb.append("name = \"" + name + "\",");
			sb.append("description = \"" + name + "\",");
			sb.append("column = \"" + column + "\",");
			if (needRequire) {
				sb.append("require = true,");
			}
			if(type.equals("Date")||needPrimary){
				sb.append("range = true,");
			}
			if (needLength) {
				sb.append("length = " + length + ", ");
			}
			sb.append("order = " + seq + ")");
			sb.append("\n");
			sb.append("private " + type + " " + StringUtil.transferCamel(column) + ";");
			sb.append("\n");
			
			sb.append("public "+type+" get"+StringUtil.upperHead(StringUtil.transferCamel(column))+"() {	return "+StringUtil.transferCamel(column)+";	}");
			sb.append("\n");
			sb.append("public void set"+StringUtil.upperHead(StringUtil.transferCamel(column))+"("+type+" "+StringUtil.transferCamel(column)+") { this."+StringUtil.transferCamel(column)+" = "+StringUtil.transferCamel(column)+"; }");
			sb.append("\n");
			sb.append("\n");
			
			if(type.equals("Date")){
				sb.append("\n");
			}else{
				sb.append("\n");
			}
			seq++;
		}
		
		sb.append("}");
		br.close();
		FileUtil.write(filePath, fileName + ".java", sb.toString());
	}

	public static void main(String[] args) throws Exception {
		
		new BeanUtil("d://DataBean", "TBlackbin").generate();
		new BeanUtil("d://DataBean", "TBlackcard").generate();
		new BeanUtil("d://DataBean", "TBlackcountry").generate();
		new BeanUtil("d://DataBean", "TBlackemail").generate();
		new BeanUtil("d://DataBean", "TBlackfinger").generate();
		new BeanUtil("d://DataBean", "TBlackip").generate();
		new BeanUtil("d://DataBean", "TBlackstate").generate();
		new BeanUtil("d://DataBean", "TBlackname").generate();
		new BeanUtil("d://DataBean", "TBlackphone").generate();
		new BeanUtil("d://DataBean", "TBlackaddr").generate();
		new BeanUtil("d://DataBean", "TBlackwebsite").generate();
		new BeanUtil("d://DataBean", "TWhitecard").generate();
		new BeanUtil("d://DataBean", "TWhiteip").generate();
		
		new BeanUtil("d://DataBean", "TBc").generate();
		new BeanUtil("d://DataBean", "TCb").generate();
		new BeanUtil("d://DataBean", "TCs").generate();
		new BeanUtil("d://DataBean", "TSc").generate();
		new BeanUtil("d://DataBean", "TSm").generate();
		new BeanUtil("d://DataBean", "TMs").generate();
		new BeanUtil("d://DataBean", "TMb").generate();
		new BeanUtil("d://DataBean", "TBm").generate();

//		new BeanUtil("d://DataBean", "TBin").generate();
//		new BeanUtil("d://DataBean", "TCurrency").generate();
//		 new BeanUtil("d://DataBean", "TBank").generate();
//		 new BeanUtil("d://DataBean", "TCard").generate();
//		 new BeanUtil("d://DataBean", "TCatalog").generate();
//		 new BeanUtil("d://DataBean", "TFunction").generate();
//		 new BeanUtil("d://DataBean", "TMerchant").generate();
//		 new BeanUtil("d://DataBean", "TNotice").generate();
//		 new BeanUtil("d://DataBean", "TOperator").generate();
//		 new BeanUtil("d://DataBean", "TOxf").generate();
//		 new BeanUtil("d://DataBean", "TResource").generate();
//		 new BeanUtil("d://DataBean", "TRole").generate();
//		 new BeanUtil("d://DataBean", "TRxf").generate();
//		 new BeanUtil("d://DataBean", "TSubject").generate();
//		 new BeanUtil("d://DataBean", "TTransaction").generate();
//		 new BeanUtil("d://DataBean", "TWebsite").generate();
//		new BeanUtil("d://DataBean", "TRisk").generate();
//		new BeanUtil("d://DataBean", "TIntercept").generate();
//		new BeanUtil("d://DataBean", "TLogic").generate();
//		new BeanUtil("d://DataBean", "TNews").generate();
//		new BeanUtil("d://DataBean", "TMail").generate();
//		new BeanUtil("d://DataBean", "TSettle").generate();
//		new BeanUtil("d://DataBean", "TBalance").generate();
//		new BeanUtil("d://DataBean", "TProduct").generate();
//		new BeanUtil("d://DataBean", "TIps").generate();
//		new BeanUtil("d://DataBean", "TMcc").generate();
		new BeanUtil("d://DataBean", "TBrand").generate();
		
	}
}

